package com.qf.service;

import com.qf.mapper.UserMapper;
import com.qf.pojo.User;

import java.util.List;

/**
 * @author lixu
 */
public class UserServiceImpl implements UserService {


    //DI : 依赖注入
    private UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

/*    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }*/

    /**
     * 查询所有用户
     * @return
     */
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    /**
     * 测试事务
     *   事务：一系列的SQL语句  要么全成功 要么全失败
     * @param user
     */
    @Override
    public void insertUser(User user) {
        //1:添加
        userMapper.insertUser(user);
  /*      try {
            Thread.sleep(3000); //ms
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        //
        int i = 1/0;//异常
        //2:修改
        userMapper.updateUserById(user);

    }
    public void updateUser(User user) {
        //1:添加
        userMapper.insertUser(user);
        //
        //int i = 1/0;//异常
        //2:修改
        userMapper.updateUserById(user);

    }

    public void deleteUser(User user) {
        //1:添加
        userMapper.insertUser(user);
        //
        //int i = 1/0;//异常
        //2:修改
        userMapper.updateUserById(user);

    }
}
